local TargetBoss	= require("app.battleV3.ui.objs.TargetBoss")
local TargetLeft 	= require("app.battleV3.ui.objs.TargetLeft")
local TargetRight 	= require("app.battleV3.ui.objs.TargetRight")

local TargetView = MClass("TargetView")

function TargetView:ctor()
	--self.root 默认为根节点
	--self:setVisible(false)

	self.targetBoss = TargetBoss.new(self:findChild("boss"))
	self.targetBossExtraHp = TargetBoss.new(self:findChild("boss_extra_hp"))
	self.targetLeft = TargetLeft.new(self:findChild("target"))

	self.targetRightTeam = TargetRight.new(self:findChild("team_target"))
	self.targetRightLeader = TargetRight.new(self:findChild("leader_target"))
	self.targetRight = self.targetRightTeam
    self:init()
end

function TargetView:init()
	self.targetRight:setVisible(false)
	self.targetLeft:setVisible(false)
	self.targetBoss:setVisible(false)
	self.targetBossExtraHp:setVisible(false)
	self.cacheDataRight = {}
--	BattleUIEvent:addEventListener(BattleUIEvent.TYPE.TARGT_UPDATE,handler(self,TargetView.updateData))
--	BattleUIEvent:addEventListener(BattleUIEvent.TYPE.TARGT_SETDATA,handler(self,TargetView.setData))
--	BattleUIEvent:addEventListener(BattleUIEvent.TYPE.TARGT_SETDATA2,handler(self,TargetView.setDataRight))
	local handle = EM:on("onSettingShowBattleTOT", function(name, isShow)
		if isShow then
        	self:setDataRight(unpack(self.cacheDataRight))
		else
			self:setDataRight(nil, nil, nil, false)
		end
    end)
    BattleUIEvent:addEmHandle(handle)
	local function onNodeEvent(event)
		if "enter" == event then
			self:onEnter()
		elseif "exit" == event then
			self:onExit()
		end
	end
	self:registerScriptHandler(onNodeEvent)
end

function TargetView:onEnter()
end

function TargetView:onExit()
	self.targetLeft:clearData()
	self.targetBoss:clearData()
	self.targetBossExtraHp:clearData()
	self.targetRightTeam:clearData()
	self.targetRightLeader:clearData()
end

function TargetView:setAsLeader(bLeader)
	if bLeader then
		self.targetRightLeader:setVisible(true)
		self.targetRightTeam:setVisible(false)
		self.targetRight = self.targetRightLeader
	else
		self.targetRightLeader:setVisible(false)
		self.targetRightTeam:setVisible(true)
		self.targetRight = self.targetRightTeam
	end
end

function TargetView:clearData()
	self.targetId = 0
	self.targetBoss:clearData()
	self.targetBoss:setVisible(false)
	self.targetBossExtraHp:clearData()
	self.targetBossExtraHp:setVisible(false)
	self.targetLeft:clearData()
	self.targetLeft:setVisible(false)
	self.targetRight:clearData()
	self.targetRight:setVisible(false)
end

function TargetView:setData(entityModel,view,targetId,bLeader)
--	print("======> TargetView:setData targetId", targetId, "self.targetId", self.targetId)
	if targetId == self.targetId then
		return
	end

	if not entityModel then
		self.targetBoss:setVisible(false)
		self.targetBossExtraHp:setVisible(false)
		self.targetLeft:setVisible(false)
		self.targetRight:setVisible(false)
		return
	end

	self:setAsLeader(bLeader)
	local actorType = view:getActorType()
	self.hasRelatedNpc = entityModel:hasRelatedNpc()

	self.targetId = targetId
	if actorType == Enums.BattleActorType.boss or entityModel:isElite() then
		if self.hasRelatedNpc then
			self.targetBoss:setVisible(false)
			self.targetLeft:setVisible(false)
			self.targetBossExtraHp:setData(entityModel,view,targetId,bLeader)
		else
			self.targetLeft:setVisible(false)
			self.targetBossExtraHp:setVisible(false)
			self.targetBoss:setData(entityModel,view,targetId,bLeader)
		end
	else
		self.targetBoss:setVisible(false)
		self.targetBossExtraHp:setVisible(false)
		self.targetLeft:setData(entityModel,view,targetId,bLeader)
	end
	self.targetRight:setVisible(UD.setting:isShowBattleTOT())
end

function TargetView:setDataRight(entityModel,view,targetId,main)
	if not targetId or not entityModel then
		self.cacheDataRight = {}
		self.targetRight:setVisible(false)
		return
	end
	if targetId == self.targetIdRight then
		return
	else
		self.targetIdRight = targetId
	end
	self.cacheDataRight[1] = entityModel
	self.cacheDataRight[2] = view
	self.cacheDataRight[3] = targetId
	self.cacheDataRight[4] = main

	if not UD.setting:isShowBattleTOT() then
		self.targetRight:setVisible(false)
		return
	end

	self.targetRight:setVisible(true)
	self.targetRight:setData(entityModel,view,targetId)
	--self.mTargetAction:gotoFrameAndPause(2)
end

function TargetView:updateData(targetId,hpMin,hpMax,manaMin,manaMax,rageMin,rageMax,energyMin,energyMax,hpExtra,hpMaxExtra)
	if not self.targetId then
		return
	end
	self.targetLeft:updateData(targetId,hpMin,hpMax,manaMin,manaMax,rageMin,rageMax,energyMin,energyMax,hpExtra,hpMaxExtra)
	if self.hasRelatedNpc then
		self.targetBossExtraHp:updateData(targetId,hpMin,hpMax,manaMin,manaMax,rageMin,rageMax,energyMin,energyMax,hpExtra,hpMaxExtra)
	else
		self.targetBoss:updateData(targetId,hpMin,hpMax,manaMin,manaMax,rageMin,rageMax,energyMin,energyMax,hpExtra,hpMaxExtra)
	end
end

function TargetView:updateDataRight(targetId,hpMin,hpMax,manaMin,manaMax,rageMin,rageMax,energyMin,energyMax)
	if not self.targetIdRight then
		return
	end
	self.targetRight:updateData(targetId,hpMin,hpMax,manaMin,manaMax,rageMin,rageMax,energyMin,energyMax)
end

function TargetView:shouldHideAllHpUI(flag)
	if not self.targetId then
		return
	end
	self.targetLeft:shouldHideAllHpUI(flag)
	if self.hasRelatedNpc then
		self.targetBossExtraHp:shouldHideAllHpUI(flag)
	else
		self.targetBoss:shouldHideAllHpUI(flag)
	end
end

function TargetView:shouldHideAllHpUIRight(flag)
	if not self.targetIdRight then
		return
	end
	self.targetRight:shouldHideAllHpUI(flag)
end

return TargetView
